<template>
  <div>
    <el-form class="input-form"
      label-position="right"
      label-width="120px"
      ref="editForm"
      :rules="propRules"
      :disabled="propReadonly"
      :model="form"
    >
    <div class="form-unit">出库明细基本信息</div>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="耗材名称" prop="matrName">
            <el-input v-model="form.matrName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="耗材品规" prop="matrSpec">
            <el-input v-model="form.matrSpec" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单品价格" prop="skuPrice">
            <el-input v-model="form.skuPrice" disabled />
          </el-form-item>
        </el-col>               
      </el-row>
      <el-row :gutter="20">        
        <el-col :span="8">
          <el-form-item label="包装单位" prop="packageUnit">
            <matr-unit-select
              ref="matrUnitGrid"
              v-model="form.packageUnit"
              propUrl="/base/matrUnit/list"
              @select="handlePackageUnitChange"
              :propMatrId="form.matrId"
              propValueField="unit"
              :propDisabled="strings.isBlank(form.matrId)"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="包装数量" prop="packageQty">
            <el-input-number v-model="form.packageQty" :min="0" :max="999" label="包装数量"></el-input-number> 
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="对应单品数量" prop="skuQty">
            <el-input v-model="form.skuQty" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="金额" prop="amount">
            <el-input v-model="form.amount" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="库位名称" prop="locationName">
            <el-input v-model="form.locationName" disabled />         
          </el-form-item>           
        </el-col>       
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="批次" prop="lot">
            <el-input v-model="form.lot" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="效期" prop="expDate">
            <el-date-picker v-model="form.expDate" disabled type="date" :format="dates.defaultDatePattern"  :picker-options="dates.pickerOptions.futureDate" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="生产日期" prop="mfgDate">
            <el-date-picker v-model="form.mfgDate" disabled type="date" :format="dates.defaultDatePattern"  :picker-options="dates.pickerOptions.date" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="灭菌日期" prop="sterilizationDate">
            <el-date-picker v-model="form.sterilizationDate" disabled type="date" :format="dates.defaultDatePattern"  :picker-options="dates.pickerOptions.date" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="注册证号" prop="certificateNo">
            <el-input v-model="form.certificateNo" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="发票号" prop="invoiceNo">
            <el-input v-model="form.invoiceNo" disabled />
          </el-form-item>
        </el-col>        
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="sn" prop="sn">
            <el-input v-model="form.sn" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="rfid" prop="rfid">
            <el-input v-model="form.rfid" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="udi" prop="udi">
            <el-input v-model="form.udi" disabled />
          </el-form-item>
        </el-col>        
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark" :autosize="{ minRows: 2, maxRows: 4}"/>
          </el-form-item>
        </el-col>
      </el-row>  
      <div class="form-unit">SN明细</div>
      <el-row>
        <div style="height:500px;overflow:auto;">
          <data-view
            v-show="form.id != null"
            propViewKey="wm_order_barcode"
            :propInitData="form"
            ref="childDataView"
            :propHidePageSearch="true"
            :propHideQueryButton="true"
          />
        </div>
      </el-row>
    </el-form>
  </div>
</template>
<script>

import MatrUnitSelect from '@/components/base/MatrUnitSelect'

export default {
  data() {
    return {
      form: {},
      viewData: {},
      initData: {},
      packageUnits: []
    }
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {   
        this.form = global.utils.jsonClone(formData)
        this.initData = initData
        let queryWrapper = this.$newWrapper()
        if (formData.id) {
          queryWrapper.eq('workOrderItemId', formData.id)
        } else {
          queryWrapper.ne(1, 1)          
        }
        this.$refs.childDataView.setQueryWrapper(queryWrapper)
      },
      afterSave: () => {
        this.propParentPage.$emit('refresh-row')
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
    propParentPage: Object
  },
  components: {
    MatrUnitSelect
  },
  watch: {
    //当包的换算比率发生变化时，重新计算单品数量和总价
    'form.packageRate': {
      handler(newVal) {
        this.form.skuQty = this.form.packageQty * newVal
        this.form.amount = this.form.skuPrice * this.form.skuQty
      },
      deep: true
    },
    //包装数量发生变化时，重新计算单品数量和总价
    'form.packageQty': {
      handler(newVal) {
        this.form.skuQty = this.form.packageRate * newVal
        this.form.amount = this.form.skuPrice * this.form.skuQty
      },
      deep: true
    },
    'form.skuQty': {
      handler(newVal) {
        if (isNaN(newVal)) {
          this.form.skuQty = '' 
        }
      },
      deep: true
    },
    'form.amount': {
      handler(newVal) {
        if (isNaN(newVal)) {
          this.form.amount = '' 
        }
      },
      deep: true
    }
  },
  methods: {
    //耗材单位变更时，重新计算换算比
    handlePackageUnitChange(unit) {
      unit && this.$set(this.form, 'packageRate', unit.rate)
    }
  }
}
</script>

<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100%;
  }
</style>
